package com.ctf.soft.ms.controls.common;

import com.ctf.soft.ms.dao.dto.manage.LoginUser;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ctf.soft.ms.service.manage.IUserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController extends BaseController {

	@Autowired
	private IUserService userService;

	@RequestMapping("/login")
	@ResponseBody
	public String loadLogin(@RequestBody ModelMap params,
			HttpServletRequest request, HttpSession session) {
		String result = "faild";
		LoginUser loginUser = null;
		String verify = session.getAttribute("VerifyCode").toString();
		String verifyCode = params.get("verifyCode").toString().trim();
		if (!verifyCode.trim().equals(verify)) {
			result = "verify";
		} else {
			String account = params.get("account").toString().trim();
			String pwd = ToolsUtil.encryptedPwd(params.get("password")
					.toString().trim());
			loginUser = userService.loadLogin(account, pwd);
			if (loginUser != null) {
				String state = loginUser.getStatus().trim();
				if (state.equals("Y")) {
					// LoginLog log = new LoginLog();
					// String pkid = ToolsUtil.getPkid();
					// log.setId(pkid);
					// log.setLoginTime(DateUtil.getCurrentTime());
					// log.setUserid(loginUser.getId().trim());
					// log.setAccount(loginUser.getAccount().trim());
					// log.setRealname(loginUser.getRealName().trim());
					// log.setLoginAddress(getIpAddress(request));
					// log.setSessionid(session.getId().trim());
					// loginLogService.save(log);
					// session.setAttribute("LOG", pkid);
					// loadPurview(session);
					session.setAttribute("UserSession", loginUser);
					result = "success";
					System.out.println("SESSIONID:" + session.getId());
				} else {
					result = "disable";
				}
			}
		}
		return result;
	}

	@RequestMapping("/logout")
	@ResponseBody
	public String loadLogout(@RequestParam("msg") String msg,
			HttpSession session) {
		String result = "faild";
		try {
			if (msg.trim().equals("ok")) {
				LoginUser loginUser = loadSession(session);
				result = loginUser.getAccount().trim();
				session.removeAttribute("UserSession");
				// String logid = (String) session.getAttribute("LOG");
				// LoginLog log = loginLogService.loadData(logid);
				// String exitTime = DateUtil.getCurrentTime();
				// log.setExitTime(exitTime);
				// long lt = DateUtil.getTime(log.getLoginTime());
				// long et = DateUtil.getTime(exitTime);
				// String times = String.valueOf((et - lt));
				// log.setLoginState(times);
				// loginLogService.update(log);
				//session.removeAttribute("LOG");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/isLogin")
	@ResponseBody
	public boolean loadIsLogin(HttpServletRequest request, HttpSession session) {
		boolean result = false;
		LoginUser loginUser = loadSession(session);
		if (loginUser != null) {
			String account = loginUser.getAccount().trim();
			Cookie[] cookies = request.getCookies();
			System.out.println("--->>>" + cookies);
			if (cookies != null) {
				for (int i = 0; i < cookies.length; i++) {
					System.out.println("--->>>" + cookies[1].getValue().trim());
					if (account.equals(cookies[1].getValue().trim())) {
						result = true;
					}
				}
			}
		}
		return result;
	}

	public String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
